package com.suneee.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.suneee.base.util.ContextHelper;
import com.suneee.base.util.JsonUtil;
import com.suneee.quartz.dao.ScheduleTimeDao;
import com.suneee.quartz.entity.TmallOrderEn;
import com.suneee.quartz.init.QuartzInit;
import com.suneee.shop.dao.ShopDao;
import com.suneee.shop.entity.Shop;
import com.suneee.store.dao.ShopStockDao;
import com.suneee.store.service.ProductService;
import com.suneee.trades.dao.PromotionDetailsDao;
import com.suneee.trades.dao.SynLogDao;
import com.suneee.trades.dao.TradesInfoDao;
import com.suneee.trades.dao.TradesOrderDao;
import com.suneee.trades.resource.SynType;
import com.suneee.trades.service.TradesInfoService;

/**
 * 订单同步任务执行类
 * @author shangwen
 *
 */
public class SynOrderQuartzJob implements StatefulJob{

	private static final Logger logger = LoggerFactory.getLogger(SynOrderQuartzJob.class);
	
	@Autowired
	private ScheduleTimeDao scheduleTimeDao;
	
	@Autowired
	private TradesInfoService tradesInfoService;
	
	@Autowired
	private ShopStockDao shopStockDao;
	
	@Autowired
	private SynLogDao synLogDao;
	
	@Autowired
	public TradesOrderDao tradesOrderDao;
	
	@Autowired
	public TradesInfoDao tradesInfoDao;
	
	@Autowired
	public ProductService productService;
	
	@Autowired
	public PromotionDetailsDao promotionDetailsDao;
	
	@Autowired
	private ShopDao shopDao;
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		scheduleTimeDao = ContextHelper.getBean("scheduleTimeDao", ScheduleTimeDao.class);
		tradesInfoService = ContextHelper.getBean("api-tradesInfo", TradesInfoService.class);
		shopStockDao = ContextHelper.getBean("shopStockDao", ShopStockDao.class);
		synLogDao = ContextHelper.getBean("synLogDao", SynLogDao.class);
		tradesOrderDao = ContextHelper.getBean("tradesOrderDao", TradesOrderDao.class);
		tradesInfoDao = ContextHelper.getBean("tradesInfoDao", TradesInfoDao.class);
		productService = ContextHelper.getBean("api-productService", ProductService.class);
		promotionDetailsDao = ContextHelper.getBean("promotionDetailsDao", PromotionDetailsDao.class);
		shopDao =  ContextHelper.getBean("shopDao", ShopDao.class);
		
		JobDataMap dataMap = context.getJobDetail().getJobDataMap();
		String jobName = context.getJobDetail().getName();
		String shopId = dataMap.getString("shopId");	
		Shop shop = shopDao.get(Integer.valueOf((shopId))); 
		
		String jsonStr = shop.getRuleInfo();
		String proxUrl = shop.getProxyURL();
		String groupTypeName = "order";
		String enterpriseCode = shop.getEnterpriseCode();
		
		logger.info("调度任务--"+jobName+" 开始时间....."+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		
		Map<String,String> shopMap = JsonUtil.parse(jsonStr);
		shopMap.put("proxUrl", proxUrl);
		try {
			//获取到店铺的交易数据
			TmallOrderEn odEntity= QuartzInit.synAllShopOrder(null,null,Integer.valueOf(shopId), enterpriseCode, groupTypeName, scheduleTimeDao, shopMap, shopStockDao,tradesInfoDao);
			//更新交易数据相关表
			
			String interva = dataMap.getString("interva");
			
			tradesInfoService.updateSynLogTable(odEntity, Integer.valueOf(shopId), enterpriseCode, odEntity.getTotalRows(), SynType.SYSTEM_AUTOMATIC_SYNCHROMIZATION.name(), 
					"系统自动",interva, tradesOrderDao, tradesInfoDao, shopStockDao, productService, promotionDetailsDao, synLogDao, scheduleTimeDao);
		} catch (Exception e) {
			logger.error("调度任务---"+jobName+"执行出错:"+e.getMessage(), e);
		}
		logger.info("调度任务--"+jobName+" 结束时间....."+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
	}

}
